﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AccountFilterTests.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the AccountFilterTests type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Data.Tests.Filters
{
    using System.Data.Objects;
    using System.Linq;
    using Data.Filters;
    using Entities;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Data.Tests.Filters.AccountFilterTests"/> type.
    /// </summary>
    [TestClass]
    public class AccountFilterTests
    {
        /// <summary>
        /// Mock database
        /// </summary>
        private readonly IObjectSet<Account> _mockObjectSet = new MockObjectSet<Account>();

        /// <summary>
        /// Initializes a new instance of the <see cref="AccountFilterTests"/> class.
        /// </summary>
        public AccountFilterTests()
        {
            _mockObjectSet.AddObject(new Account { Id = 1, Name = "Account 1", AccountType = 1, });
            _mockObjectSet.AddObject(new Account { Id = 2, Name = "Account 2", AccountType = 2, });
            _mockObjectSet.AddObject(new Account { Id = 3, Name = "Account 3", AccountType = 3, });
        }

        /// <summary>
        /// Filter_s the should return everything if the object to comare is null.
        /// </summary>
        [TestMethod]
        public void AccountFilterShouldReturnEverythingIfTheObjectToComareIsNull()
        {
            var result = _mockObjectSet.Filter(null).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Count());
        }

        /// <summary>
        /// Filters the should return everything if the object to comare is uninitialized.
        /// </summary>
        [TestMethod]
        public void AccountFilterShouldReturnEverythingIfTheObjectToComareIsUninitialized()
        {
            var result = _mockObjectSet.Filter(new Account()).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Count());
        }

        /// <summary>
        /// Filters the should return object filtered by id.
        /// </summary>
        [TestMethod]
        public void AccountFilterShouldReturnObjectFilteredById()
        {
            var result = _mockObjectSet.Filter(new Account { Id = 2 }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(2, result[0].Id);
            Assert.AreEqual("Account 2", result[0].Name);
        }

        /// <summary>
        /// Filters the name of the should return object filtered by.
        /// </summary>
        [TestMethod]
        public void AccountFilterShouldReturnObjectFilteredByName()
        {
            var result = _mockObjectSet.Filter(new Account { Name = "Account 1" }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(1, result[0].Id);
            Assert.AreEqual("Account 1", result[0].Name);
        }

        /// <summary>
        /// Filters the should return object filtered by name with different case.
        /// </summary>
        [TestMethod]
        public void AccountAccountFilterShouldReturnObjectFilteredByNameWithDifferentCase()
        {
            var result = _mockObjectSet.Filter(new Account { Name = "Account 2" }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(2, result[0].Id);
            Assert.AreEqual("Account 2", result[0].Name);
        }

        /// <summary>
        /// Filters the type of the should return object filtered by account.
        /// </summary>
        [TestMethod]
        public void AccountFilterShouldReturnObjectFilteredByAccountType()
        {
            var result = _mockObjectSet.Filter(new Account { AccountType = 2 }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(2, result[0].Id);
            Assert.AreEqual("Account 2", result[0].Name);
            Assert.AreEqual(2, result[0].AccountType);
        }

        /// <summary>
        /// Filters the should return object filtered by combination of fields.
        /// </summary>
        [TestMethod]
        public void AccountFilterShouldReturnObjectFilteredByCombinationOfFields()
        {
            var result = _mockObjectSet.Filter(new Account { Name = "Account 3", AccountType = 3 }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(3, result[0].Id);
            Assert.AreEqual("Account 3", result[0].Name);
            Assert.AreEqual(3, result[0].AccountType);
        }

        /// <summary>
        /// Filters the should return nothing if mismatching object filtered by combination of fields.
        /// </summary>
        [TestMethod]
        public void AccountFilterShouldReturnNothingIfMismatchingObjectFilteredByCombinationOfFields()
        {
            var result = _mockObjectSet.Filter(new Account { Name = "Account 2", AccountType = 3 }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count());
        }
    }
}